package com.xmad.ksadutils.AdSDK;

import android.app.Activity;
import android.content.Context;
import android.widget.FrameLayout;

import androidx.fragment.app.Fragment;

import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.SdkConfig;
import com.xmad.ksadutils.AdSDK.AdBase.AdBase;
import com.xmad.ksadutils.AdSDK.AdBase.AdListener;
import com.xmad.ksadutils.AdSDK.AdBase.Manufactor;
import com.xmad.ksadutils.AdSDK.AdBase.TTAdManagerHolder;
import com.xmad.ksadutils.AdSDK.Utlis.SpUtils;
import com.xmad.ksadutils.R;

import java.util.List;

/**
 * @Author: 郝家伟
 * @CreateDate: 2022/3/28
 */
public class AdTool {
    private volatile static AdTool mInstance;
    public AdBase baseLoad;
    Activity context;

    /**
     * 单例模式
     *
     * @return
     */
    public static AdTool getInstance() {
        if (mInstance == null) {
            synchronized (AdTool.class) {
                if (mInstance == null) {
                    mInstance = new AdTool();
                }
            }
        }
        return mInstance;
    }
    public AdTool get(Activity context){
        this.context=context;
        return this;
    }




    public AdTool get(Fragment fragment){
        this.context=fragment.getActivity();
        return this;
    }


    public AdTool setAdLoad(AdBase baseLoad) {
        this.baseLoad = baseLoad;
        return this;
    }


    public AdTool setListener(AdListener adListener) {
        this.baseLoad.setListener(adListener);
        return this;
    }




    public AdTool setPGid(boolean is, String... PGid) {
        baseLoad.setActivity(getContext());
        baseLoad.setPGid(PGid);
        baseLoad.setReveal(is);
        baseLoad.load_();
        return this;
    }

    public AdTool setBdid(boolean is, String... bdid) {
        baseLoad.setActivity(getContext());
        baseLoad.setBdid(bdid);
        baseLoad.setReveal(is);
        baseLoad.load_();
        return this;
    }

    public AdTool setKSid(boolean is, Long... KSid) {
        baseLoad.setActivity(getContext());
        baseLoad.setKSid(KSid);
        baseLoad.setReveal(is);
        baseLoad.load_();
        return this;
    }
//
    public AdTool setKSid(boolean is, String [] KSid) {
        baseLoad.setActivity(getContext());
        baseLoad.setKSid(KSid);
        baseLoad.setReveal(is);
        baseLoad.load_();
        return this;
    }




    public AdTool setFrameLayout(FrameLayout frameLayout) {
        baseLoad.setFrameLayout(frameLayout);
        return this;
    }


    public static void init(Context context, Manufactor manufactor){
        if (manufactor.getKwai()!=null&&!manufactor.getKwai().equals("")){
            initKSSDK(context,manufactor.getKwai());
        }
        if (manufactor.getPangolin()!=null&&!manufactor.getPangolin().equals("")){
            initPGSDK(context,manufactor.getPangolin());
        }
        SpUtils.getInstance().init(context);
    }

    public static void init(Context context, Manufactor manufactor,InitFactory initFactory){
        init(context,manufactor);
        initFactory.init();
    }

    public static void initPGSDK(Context context, String appId) {
        TTAdManagerHolder.init(context,appId);
    }

    public static void initKSSDK(Context context, String appId) {
        KsAdSDK.init(context, new SdkConfig.Builder()
                .appId(appId) // 测试aapId，请联系快手平台申请正式AppId，必填
                .appName((context.getString(R.string.app_name))) // 测试appName，请填写您应用的名称，非必填
//                .appKey(APP_KEY) // 直播sdk安全验证，接入直播模块必填
//                .appWebKey(APP_WB_KEY) // 直播sdk安全验证，接入直播模块必填
                // Feed和入口组件，夜间模式样式配置，如果不配置 默认是"ks_adsdk_night_styles.xml"
                .nightThemeStyleAssetsFileName("ks_adsdk_night_styles.xml")
                .showNotification(true) // 是否展示下载通知栏
                .debug(true)
                .build());
    }

    public  interface InitFactory{
          void init();
    }

    public Activity getContext() {
        return context;
    }


    public static String[] get(List<String> list) {
        String[] ids = list.toArray(new String[list.size()]);
        return ids;
    }


}
